package com.project.service.impl;

import com.project.bean.UnitBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IUnitMapper;
import com.project.service.IUnitService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UnitServiceImpl implements IUnitService {

    @Autowired
    private IUnitMapper mapper;
    @Autowired
    private IRoomMapper roomMapper;

    @Override
    public void add(UnitBean unit, Integer roomId) {
        mapper.add(unit);
       roomMapper.updateState(roomId,"设备损坏");

    }

    @Override
    public List<UnitBean> findAll(Integer roomId) {
        return mapper.findAll(roomId);
    }

    @Override
    public UnitBean findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public void update(Integer id, Integer roomId) {
        if (mapper.findById(id) == null){
            return;
        }
        mapper.updateState(id,"已解决");

        if (mapper.countByRoom("未解决",roomId) == 0){
            roomMapper.updateState(roomId,"正常");
        }
    }
}